package cn.hesion.operasystem;
/**
 * ClassName: PageReplacementAlg <br/>
 * Description: <br/>
 * date: 2022/4/30 8:21<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 页面置换算法实现
 *
 * @author: hesion
 * @create: 2022-04-30 08:21
 **/
public class PageReplacementAlg {
    //请求序列
    static private List<Integer> reqPageList = new ArrayList();

    //内存大小
    static int size = 6;

    static {
        for (int i = 0; i < 20; i++) {
            reqPageList.add(new Random().nextInt(10));
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        reqPageList.stream().forEach(e -> System.out.print(" " + e));
        System.out.println();
    }

    /**
     * FIFO算法
     */
    void fifoStrtagy() {
        System.out.println("=========================================");
        System.out.println("================FIFO=====================");
        System.out.println("=========================================");
        //记录相关数据
        AtomicInteger allPageNum = new AtomicInteger();
        AtomicInteger missPageNum = new AtomicInteger();
        AtomicInteger replacePageNum = new AtomicInteger();
        allPageNum.set(0);
        missPageNum.set(0);
        replacePageNum.set(0);
        //分配队列
        Queue<Integer> blocks = new ArrayDeque<>();

        //开始模拟内存的分配，循环请求序列
        reqPageList.stream().forEach(e -> {
            System.out.println("currentPage:" + e);
            allPageNum.incrementAndGet();
            //判断是否在内存中
            if (!blocks.contains(e)) {
                //缺页
                missPageNum.incrementAndGet();
                if (blocks.size() == size) {
                    replacePageNum.incrementAndGet();
                    blocks.poll();
                }
                blocks.add(e);
            }

            System.out.println("memory:" + (size - blocks.size()) + " block:" + blocks + " ");
            System.out.println("allPageNum:" + (allPageNum.get()) + " missPageNum:" + (missPageNum.get()) + " missPageRate:" + (Float.parseFloat(missPageNum + "") / Float.parseFloat(allPageNum.get() + "")));
        });

    }

    void lruStrategy() {
        System.out.println("=========================================");
        System.out.println("================LRU=====================");
        System.out.println("=========================================");
        //记录相关数据
        AtomicInteger allPageNum = new AtomicInteger();
        AtomicInteger missPageNum = new AtomicInteger();
        AtomicInteger replacePageNum = new AtomicInteger();
        allPageNum.set(0);
        missPageNum.set(0);
        replacePageNum.set(0);

        Stack<Integer> stack = new Stack<>();

        reqPageList.stream().forEach(e -> {
            allPageNum.incrementAndGet();
            System.out.println("currentPage:" + e);
            System.out.println("stack0..");
            stack.stream().forEach(e1-> System.out.print(e1+" "));
            System.out.println();
            if (!stack.contains(e)) {
                missPageNum.incrementAndGet();

                if (stack.size() == size) {
                    replacePageNum.incrementAndGet();
                    stack.remove(0);
                }
            }else {
                stack.remove((Object)e);
            }
            stack.add(e);
            System.out.println("stack1..");
            stack.stream().forEach(e1-> System.out.print(e1+" "));
            System.out.println();
            System.out.println("memory:" + (size - stack.size()) + " stack:" + stack + " ");
            System.out.println("allPageNum:" + (allPageNum.get()) + " missPageNum:" + (missPageNum.get()) + " missPageRate:" + (Float.parseFloat(missPageNum + "") / Float.parseFloat(allPageNum.get() + "")));
        });
    }

    public static void main(String[] args) {
        PageReplacementAlg pageReplacementAlg = new PageReplacementAlg();
//        pageReplacementAlg.fifoStrtagy();
        pageReplacementAlg.lruStrategy();
    }
}
